package com.jy.rhin.application.cda;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.jiankangda.*;
import com.jy.rhin.infra.cda.service.JianKangTJService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.JianKangTJ;
import com.jy.rhin.model.ValueUnit;
import com.jy.rhin.model.body.jiankangtj.*;
import com.jy.rhin.model.body.zhengzhuang.ZhengZhuang;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 健康体检
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class JianKangTJApplication {
    private static final String FTL_PATH = "/16jiankangtj.ftl";
    private static final String TRUE = "true";
    private static final String FALSE = "false";
    @Resource
    private CDACreator cdaCreator;
  /*  @Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private JianKangTJService jianKangTJService;
    @Resource
    private PatientService patientService;
    @Resource
    private ShuYuService shuYuService;

    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String yeWuJGDM, String tiJianBH) {
        return cdaCreator.create(createJianKangTJ(yeWuJGDM, tiJianBH), FTL_PATH);
    }

    private ClinicalDocumentData createJianKangTJ(String yeWuJGDM, String tiJianBH) {
        JianKangTJVo jianKangTJ = jianKangTJService.getJianKangTJ(yeWuJGDM, tiJianBH);
        if (Objects.isNull(jianKangTJ) || Objects.isNull(jianKangTJ.getJkTiJianJBXX())) {
            throw new CDAException("主业务数据不存在", null);
        }
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();

        BingRenXXDto bingRenInfo = patientService.queryPatientById(jkTiJianJBXX.getPatientId());
        if (Objects.isNull(bingRenInfo)) {
            throw new CDAException("患者信息不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(jkTiJianJBXX.getYEWUJGDM()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(jkTiJianJBXX.getYEWUJGDM()).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }
        Map<String, List<DaiMaDto>> shuYuMap = shuYuZhiYuQuery(bingRenInfo, jianKangTJ);

        JianKangTJ docData = new JianKangTJ();
        setClinicalDocumentData(docData, jianKangTJ, bingRenInfo, hospital);
        setLegalAuthenticator(jianKangTJ, docData);
        setZhengZhuang(shuYuMap, jianKangTJ, docData);
        setShengMingTZ(shuYuMap, jianKangTJ, docData);
        setKouQiang(shuYuMap, jianKangTJ, docData);
        setYanBu(shuYuMap, jianKangTJ, docData);
        setFuBu(shuYuMap, jianKangTJ, docData);
        setGongNengJC(shuYuMap, jianKangTJ, docData);
        setHuXiXT(shuYuMap, jianKangTJ, docData);
        setShengZhiQ(shuYuMap, jianKangTJ, docData);
        setShiYanSJC(shuYuMap, jianKangTJ, docData);
        setFuZhuJC(shuYuMap, jianKangTJ, docData);
        setZhenDuanJL(shuYuMap, jianKangTJ, docData);
        setYongYao(shuYuMap, jianKangTJ, docData);
        setJiaZuS(shuYuMap, jianKangTJ, docData, bingRenInfo);
        setZhuYuanS(shuYuMap, jianKangTJ, docData);
        setBingChuangS(shuYuMap, jianKangTJ, docData);
        setYuFangJZ(shuYuMap, jianKangTJ, docData);
        setZhiYeBLS(shuYuMap, jianKangTJ, docData);
        setShengHuoFS(shuYuMap, jianKangTJ, docData);
        setJianKang(shuYuMap, jianKangTJ, docData);

        // 皮肤
        PiFu piFu = new PiFu();
        String daiMa = strDefaultIfEmpty(jkTiJianJBXX.getPIFU(), "01");
        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_004);
        piFu.setPiFuJC(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
        docData.setPiFu(piFu);

        setDefaultJianKangTJ(docData);
        return docData;
    }

    private void setZhengZhuang(Map<String, List<DaiMaDto>> shuYuMap,
                                JianKangTJVo jianKangTJ, JianKangTJ docData) {
        // 症状 CVX_ZZDM
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        String daiMa = jkTiJianJBXX.getZHENZHUANG();
        List<ZhengZhuang> zhengZhuangs = new ArrayList<>();
        Map<String, String> guoJiaDMMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.ICD_10);

        List<String> daiMaList = listDaiMa(daiMa);
        for (String daiMaItem : daiMaList) {
            // 平台代码值
            ZhengZhuangDMYSM zhengZhuangDMYSM = ZhengZhuangDMYSM.getByPingTaiDM(daiMaItem);

            // 获取国家代码
            String guoJiaDMV = guoJiaDMMap.get(zhengZhuangDMYSM.getGuoJiaDM());

            ZhengZhuang item = new ZhengZhuang();
            item.setZhengZhuangDM(zhengZhuangDMYSM.getGuoJiaDM());
            item.setZhengZhuangMC(guoJiaDMV);
            zhengZhuangs.add(item);
        }
        docData.setZhengZhuangList(zhengZhuangs);
    }

    public void setShengMingTZ(Map<String, List<DaiMaDto>> shuYuMap,
                               JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            log.warn("jkTiJianJBXX is null");
            return;
        }
        // 生命体征
        ShengMingTZ shengMingTZ = new ShengMingTZ();
        shengMingTZ.setMaiLV(String.valueOf(jkTiJianJBXX.getMAILV()));
        String shengao = String.valueOf(jkTiJianJBXX.getSHENGAO().setScale(1, BigDecimal.ROUND_HALF_UP));
        //默认值
        if (jkTiJianJBXX.getSHENGAO().compareTo(BigDecimal.ZERO) < 0) {
            shengao = "160.0";
        }
        shengMingTZ.setShenGao(shengao);
        shengMingTZ.setTiWen(String.valueOf(jkTiJianJBXX.getTIWEN()));
        shengMingTZ.setTiZhong(String.valueOf(jkTiJianJBXX.getTIZHONG().setScale(1, RoundingMode.DOWN)));
        shengMingTZ.setHuXiPL(String.valueOf(jkTiJianJBXX.getHUXIPL()));
        shengMingTZ.setTiZhiZS(String.valueOf(jkTiJianJBXX.getTIZHIZS()));
        shengMingTZ.setXinLv(String.valueOf(jkTiJianJBXX.getXINGZHANGXL()));
        shengMingTZ.setYaoWei(String.valueOf(jkTiJianJBXX.getYAOWEI().setScale(1, RoundingMode.DOWN)));
        shengMingTZ.setYouCeShuZhangY(String.valueOf(jkTiJianJBXX.getYOUCESZY()));
        shengMingTZ.setZuoCeShuZhangY(String.valueOf(jkTiJianJBXX.getZUOCESZY()));
        shengMingTZ.setYouCeShouSuoY(String.valueOf(jkTiJianJBXX.getYOUCESSY()));
        shengMingTZ.setZuoCeShouSuoY(String.valueOf(jkTiJianJBXX.getZUOCESSY()));
        docData.setShengMingTZ(shengMingTZ);
    }

    public void setKouQiang(Map<String, List<DaiMaDto>> shuYuMap,
                            JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        // 口腔、咽喉和牙齿章节
        KouQiang kouQiang = new KouQiang();
        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_007);
        String daiMa = strDefaultIfEmpty(jkTiJianJBXX.getKOUCHUN(), "9");
        kouQiang.setKouChun(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_010);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getCHILIE(), "1");
        daiMa = listDaiMa(daiMa).stream().findFirst().orElse("1");
        kouQiang.setChiLie(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        kouQiang.setChiLieMS(daiMaDtoMap.getOrDefault(jkTiJianJBXX.getCHILIE(), "1"));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_YB);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getYANBU(), "1");
        kouQiang.setYanBu(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        docData.setKouQiang(kouQiang);
    }


    public void setYanBu(Map<String, List<DaiMaDto>> shuYuMap,
                         JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }

        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_006);
        // 眼章节
        YanBu yanBu = new YanBu();
        yanBu.setYanDi("2".equals(jkTiJianJBXX.getYANDI()) ? TRUE : FALSE);
        yanBu.setYanDiYCXS(daiMaDtoMap.getOrDefault(jkTiJianJBXX.getYANDIYCXS(), "-"));

        yanBu.setGongMo(new CodeObject(jkTiJianJBXX.getGONGMO(), daiMaDtoMap.get(jkTiJianJBXX.getGONGMO())));
        yanBu.setYouYanJZYSLZ(String.valueOf(jkTiJianJBXX.getYOUYANJZYSLZ()));
        yanBu.setYouYanLYYSLZ(String.valueOf(jkTiJianJBXX.getYOUYANLYYSLZ()));
        yanBu.setZuoYanJZYSLZ(String.valueOf(jkTiJianJBXX.getZUOYANJZYSLZ()));
        yanBu.setZuoYanLYYSLZ(String.valueOf(jkTiJianJBXX.getZUOYANLYYSLZ()));
        docData.setYanBu(yanBu);

        // 耳章节
        String tingli = jkTiJianJBXX.getTINGLI();
        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_TINGLI);
        docData.setTingLi(new CodeObject(tingli, daiMaDtoMap.get(tingli)));
    }

    public void setFuBu(Map<String, List<DaiMaDto>> shuYuMap,
                        JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }
        FuBu fuBu = new FuBu();
        fuBu.setFuBuBKXS(strDefaultIfEmpty(jkTiJianJBXX.getFUBUBKXS(), "无"));
        fuBu.setFuBuBK("2".equals(jkTiJianJBXX.getFUBUBK()) ? TRUE : FALSE);
        fuBu.setFuBuYT("2".equals(jkTiJianJBXX.getFUBUYT()) ? TRUE : FALSE);
        fuBu.setFuBuYTXS(strDefaultIfEmpty(jkTiJianJBXX.getFUBUYTXS(), "无"));

        fuBu.setFuBuGD("2".equals(jkTiJianJBXX.getFUBUGD()) ? TRUE : FALSE);
        fuBu.setFuBuGDXS(strDefaultIfEmpty(jkTiJianJBXX.getFUBUGDXS(), "无"));
        fuBu.setFuBuPD("2".equals(jkTiJianJBXX.getFUBUPD()) ? TRUE : FALSE);
        fuBu.setFuBuPDXS("2".equals(jkTiJianJBXX.getFUBUPDXS()) ? TRUE : FALSE);
        fuBu.setFuBuYDXZY("2".equals(jkTiJianJBXX.getFUBUYDXZY()) ? TRUE : FALSE);
        fuBu.setFuBuYDXZYXS(strDefaultIfEmpty(jkTiJianJBXX.getFUBUYDXZYXS(), "无"));
        docData.setFuBu(fuBu);
    }

    public void setGongNengJC(Map<String, List<DaiMaDto>> shuYuMap,
                              JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }

        // 功能检查章节
        GongNengJC gongNengJC = new GongNengJC();

        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_YDGN);
        String daiMa = strDefaultIfEmpty(jkTiJianJBXX.getYUNDONGGN(), "1");
        gongNengJC.setYunDongQK(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
        docData.setGongNengJC(gongNengJC);

        // 心脏章节
        XinZhang xinZhang = new XinZhang();
        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_XINLVQDM);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getXINGZHANGXLV(), "1");
        xinZhang.setXinZhangXL(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        xinZhang.setXinZhangZY("2".equals(jkTiJianJBXX.getXINGZHANGZY()) ? TRUE : FALSE);
        xinZhang.setXinZhangZYXS(strDefaultIfEmpty(jkTiJianJBXX.getXINGZHANGZYXS(), "无"));
        docData.setXinZhang(xinZhang);

        XueGuan xueGuan = new XueGuan();
        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_015);
        daiMa = jkTiJianJBXX.getZUBEIDMBD();
        xueGuan.setZuBeiDMBD(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
        docData.setXueGuan(xueGuan);
    }


    private void setHuXiXT(Map<String, List<DaiMaDto>> shuYuMap,
                           JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }

        // 呼吸系统
        HuXiXT huXiXT = new HuXiXT();
        huXiXT.setFeiBuLYXS(strDefaultIfEmpty(jkTiJianJBXX.getFEIBULYQTXS(), "无"));
        huXiXT.setFeiBuLY("2".equals(jkTiJianJBXX.getFEIBULY()) ? FALSE : TRUE);
        huXiXT.setTongZhuangGXBZ("2".equals(jkTiJianJBXX.getTONGZHUANGXBZ()) ? TRUE : FALSE);
        huXiXT.setFeiBuHXY("2".equals(jkTiJianJBXX.getFEIBUHXY()) ? TRUE : FALSE);
        huXiXT.setFeiBuHXYYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFEIBUHXYYCXS(), "无"));
        docData.setHuXiXT(huXiXT);

        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_011);
        String daiMa = strDefaultIfEmpty(jkTiJianJBXX.getLINGBAJ(), "9");
        docData.setLinBaJC(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_014);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getXIAZHISZ(), "1");
        docData.setXiaZhiSZ(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_013);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getGANGMENZZ(), "1");
        docData.setGangMenZZ(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_10_012);
        daiMa = strDefaultIfEmpty(jkTiJianJBXX.getRUXIAN(), "1");
        daiMa = listDaiMa(daiMa).stream().findFirst().orElse("1");
        docData.setRuXian(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
    }

    private void setShengZhiQ(Map<String, List<DaiMaDto>> shuYuMap,
                              JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }
        // 生殖器章节
        ShengZhiQ shengZhiQ = new ShengZhiQ();
        shengZhiQ.setFuKeFJYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFUKEFJYCXS(), "无"));
        shengZhiQ.setFuKeFJ("2".equals(jkTiJianJBXX.getFUKEFJ()) ? TRUE : FALSE);
        shengZhiQ.setFuKeGTYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFUKEGTYCXS(), "无"));
        shengZhiQ.setFuKeGT("2".equals(jkTiJianJBXX.getFUKEGT()) ? TRUE : FALSE);
        shengZhiQ.setFuKeGJ("2".equals(jkTiJianJBXX.getFUKEGJ()) ? TRUE : FALSE);
        shengZhiQ.setFuKeGJYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFUKEGJYCXS(), "无"));
        shengZhiQ.setFuKeYD("2".equals(jkTiJianJBXX.getFUKEYD()) ? TRUE : FALSE);
        shengZhiQ.setFuKeYDYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFUKEYDYCXS(), "无"));
        shengZhiQ.setFuKeWY("2".equals(jkTiJianJBXX.getFUKEWY()) ? TRUE : FALSE);
        shengZhiQ.setFuKeWYYCXS(strDefaultIfEmpty(jkTiJianJBXX.getFUKEWYYCXS(), "无"));
        docData.setShengZhiQ(shengZhiQ);
    }

    private void setShiYanSJC(Map<String, List<DaiMaDto>> shuYuMap,
                              JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianFZJCVo data = jianKangTJ.getJkTiJianFZJCList().stream().findFirst().orElse(null);
        if (Objects.isNull(data)) {
            log.warn("jkTiJianFZJC is null");
            return;
        }
        String daiMa = "";
        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_50_015);
        ShiYanSJC shiYanSJC = new ShiYanSJC();
        shiYanSJC.setXueHongDB(String.valueOf(data.getXUEHONGDB()));
        shiYanSJC.setBaiXiB(bigDecimalToStr(data.getBAIXIB(), 1));
        shiYanSJC.setXueXiaoB(bigDecimalToStr(data.getXUEXIAOB(), 0));
        daiMa = data.getNIAODANBBDXJCJGDM();
        shiYanSJC.setNiaoDanBBDXJCJG(new CodeObject(daiMa, daiMaDtoMap.getOrDefault(daiMa, "")));

        daiMa = data.getNIAOTONGTDXJCJGDM();
        shiYanSJC.setNiaoTongTDXJCJG(new CodeObject(daiMa, daiMaDtoMap.getOrDefault(daiMa, "")));

        daiMa = data.getNIAOQIANXDXJCJGDM();
        shiYanSJC.setNiaoQianXDXJCJG(new CodeObject(daiMa, daiMaDtoMap.getOrDefault(daiMa, "")));
        shiYanSJC.setKongFuXT(bigDecimalToStr(data.getKONGFUXT(), 1));
        shiYanSJC.setNiaoWeiLBDB(bigDecimalToStr(data.getNIAOWEILBDB(), 1));

        String daBianQX = strDefaultIfEmpty(data.getDABIANQX(), "1");
        shiYanSJC.setDaBianQX("1".equals(daBianQX) ? "ture" : "false");
        shiYanSJC.setTangHuaXHDB(bigDecimalToStr(data.getTANGHUAXHDB(), 1));
        shiYanSJC.setXueQingGBZAM(bigDecimalToStr(data.getXUEQINGGBZAM(), 0));
        shiYanSJC.setXueQingGCZAM(String.valueOf(data.getXUEQINGGCZAM()));
        shiYanSJC.setBaiDanB(bigDecimalToStr(data.getBAIDANB(), 0));
        shiYanSJC.setZongDanHS(bigDecimalToStr(data.getZONGDANHS(), 1));
        shiYanSJC.setJieHeDHS(bigDecimalToStr(data.getJIEHEDHS(), 1));
        shiYanSJC.setXueQingJG(bigDecimalToStr(data.getXUEQINGJG(), 1));
        shiYanSJC.setXueNiaoSU(bigDecimalToStr(data.getXUENIAOSU(), 1));
        shiYanSJC.setXueJiaND(bigDecimalToStr(data.getXUEJIAND(), 1));
        if (data.getXUENAND() != null) {
            BigDecimal bigDecimal = data.getXUENAND().setScale(2, BigDecimal.ROUND_HALF_UP);
            shiYanSJC.setXueNaND(bigDecimal.toString());
        } else {
            shiYanSJC.setXueNaND("0.00");
        }
        shiYanSJC.setZongDanGC(String.valueOf(data.getZONGDANGC()));
        if (data.getGANYOUSZ() != null) {
            BigDecimal bigDecimal = data.getGANYOUSZ().setScale(1, BigDecimal.ROUND_HALF_UP);
            shiYanSJC.setGanYouSZ(bigDecimal.toString());
        } else {
            shiYanSJC.setGanYouSZ("0.0");
        }
        shiYanSJC.setXueQingDMDZDBDGC(String.valueOf(data.getXUEQINGDMDZDBDGC()));
        shiYanSJC.setXueQingGMDZDBDGC(String.valueOf(data.getXUEQINGGMDZDBDGC()));

        daiMa = strDefaultIfEmpty(data.getYIGANGYBMKY(), "1");
        shiYanSJC.setYiGanGYBMKY(new CodeObject(daiMa, "1".equals(daiMa) ? "阴性" : "阳性"));
        shiYanSJC.setYiXingGDBMKT(new CodeObject("1", "阴性"));
        shiYanSJC.setYiXingGDEKY(new CodeObject("1", "阴性"));
        shiYanSJC.setYiXingGYEKT(new CodeObject("1", "阴性"));
        shiYanSJC.setYiXingGDHXKT(new CodeObject("1", "阴性"));

        daiMa = strDefaultIfEmpty(data.getNIAOTANGDXJCJGDM(), "01");
        shiYanSJC.setNiaoTangDXJCJG(new CodeObject(daiMa, daiMaDtoMap.getOrDefault(daiMa, "0")));

        docData.setShiYanSJC(shiYanSJC);
    }

    private String bigDecimalToStr(BigDecimal bigDecimal, int scale) {
        return bigDecimalToStr(bigDecimal, "0", scale);
    }

    private String bigDecimalToStr(BigDecimal bigDecimal, String defaultStr) {
        return bigDecimalToStr(bigDecimal, defaultStr, 2);
    }

    /**
     * 格式化字段数据
     */
    private String bigDecimalToStr(BigDecimal bigDecimal, String defaultStr, int scale) {
        bigDecimal = Optional.ofNullable(bigDecimal).orElse(new BigDecimal(defaultStr))
                .setScale(scale, RoundingMode.DOWN);
        return bigDecimal.toString();
    }

    private void setFuZhuJC(Map<String, List<DaiMaDto>> shuYuMap,
                            JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianFZJCVo jkTiJianFZJC = jianKangTJ.getJkTiJianFZJCList().stream().findFirst().orElse(null);
        if (Objects.isNull(jkTiJianFZJC)) {
            log.warn("jkTiJianFZJC is null");
            return;
        }
        FuZhuJC fuZhuJC = new FuZhuJC();
        fuZhuJC.setXiongBUXXP("2".equals(jkTiJianFZJC.getXIONGBUXXP()) ? TRUE : FALSE);
        fuZhuJC.setXiongBuXXPYCXS(jkTiJianFZJC.getXIONGBUXXPYCXS());
        fuZhuJC.setXinDianT("2".equals(jkTiJianFZJC.getXINDIANT()) ? TRUE : FALSE);
        fuZhuJC.setXinDianTYCXS(strDefaultIfEmpty(jkTiJianFZJC.getXINDIANTYCXS(), "无"));
        fuZhuJC.setFuBuBC("2".equals(jkTiJianFZJC.getFUBUBC()) ? TRUE : FALSE);
        fuZhuJC.setFuBuBCYCXS(jkTiJianFZJC.getFUBUBCYCXS());
        fuZhuJC.setGongJingTP("2".equals(jkTiJianFZJC.getGONGJINGTP()) ? TRUE : FALSE);
        fuZhuJC.setGongJingTPYCXS(strDefaultIfEmpty(jkTiJianFZJC.getGONGJINGTPYCXS(), "无"));
        docData.setFuZhuJC(fuZhuJC);
    }

    private void setZhenDuanJL(Map<String, List<DaiMaDto>> shuYuMap,
                               JianKangTJVo jianKangTJ, JianKangTJ docData) {
        List<JkTiJianXCZYJKWTVo> listJKWT = jianKangTJ.getJkTiJianXCZYJKWTList();
        if (CollectionUtils.isEmpty(listJKWT)) {
            log.warn("JkTiJianXCZYJKWT is null");
            return;
        }

        // 诊断记录章节
        ZhenDuanJL zhenDuanJL = new ZhenDuanJL();
        for (JkTiJianXCZYJKWTVo jkwt : listJKWT) {
            if (!StringUtil.isNullOrWhiteSpace(jkwt.getNAOXUEGJB())) {
                if (jkwt.getNAOXUEGJB().contains("3")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("I64.x00", "脑卒中"));
                    break;
                }
                if (jkwt.getNAOXUEGJB().contains("4")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("Z09.804", "蛛网膜下腔出血治疗后随诊检查"));
                    break;
                }
                if (jkwt.getNAOXUEGJB().contains("5")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("G45.900", "短暂性大脑缺血性发作"));
                    break;
                }
            }
            if (!StringUtil.isNullOrWhiteSpace(jkwt.getSHENZANGJB())) {
                if (jkwt.getSHENZANGJB().contains("2") || jkwt.getSHENZANGJB().contains("3")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("E14.200", "糖尿病伴有肾的并发症"));
                    break;
                }
                if (jkwt.getSHENZANGJB().contains("4")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("N00.900", "急性肾炎综合征"));
                    break;
                }
                if (jkwt.getSHENZANGJB().contains("5")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("N03.900", "慢性肾炎综合征"));
                    break;
                }
            }
            if (!StringUtil.isNullOrWhiteSpace(jkwt.getXINZANGJB())) {
                if (jkwt.getXINZANGJB().contains("2")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("I20.006", "心肌梗死前综合征"));
                    break;
                }
                if (jkwt.getXINZANGJB().contains("3")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("I20.900", "心绞痛"));
                    break;
                }
                if (jkwt.getXINZANGJB().contains("5")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("I50.000", "充血性心力衰竭"));
                    break;
                }
            }
            if (!StringUtil.isNullOrWhiteSpace(jkwt.getXUEGUANJB())) {
                if (jkwt.getXUEGUANJB().contains("2")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("I72.816", "腹腔动脉夹层动脉瘤"));
                    break;
                }
            }
            if (!StringUtil.isNullOrWhiteSpace(jkwt.getYANBUJB())) {
                if (jkwt.getYANBUJB().contains("2")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("H35.600", "视网膜出血"));
                    break;
                }
                if (jkwt.getYANBUJB().contains("4")) {
                    zhenDuanJL.setXianCunZYJKWT(new CodeObject("H26.900", "白内障"));
                    break;
                }
            }
        }

        List<JkLaoNianRZYJKGLVo> list = jianKangTJ.getJkLaoNianRZYJKGLList();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("JkLaoNianRZYJKGL is null");
            return;
        }

        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV05_01_005);
        Map<String, String> daiMaDtoMap2 = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_ZYTZFLPD);
        for (JkLaoNianRZYJKGLVo item : list) {
            ZhenDuanJL.ZhongYiTZ zhongYiT = new ZhenDuanJL.ZhongYiTZ();
            if ("-".equals(item.getZHUYAOTZ()) || StringUtil.isNullOrWhiteSpace(item.getQINGXIANGTZ())) {
                continue;
            }
            zhongYiT.setTiZhiFL(new CodeObject(item.getZHUYAOTZ(), daiMaDtoMap.get(item.getZHUYAOTZ())));
            zhongYiT.setTiZhiFLPDJG(new CodeObject(item.getQINGXIANGTZ(), daiMaDtoMap2.get(item.getQINGXIANGTZ())));
            zhenDuanJL.getZhongYiTZList().add(zhongYiT);
        }
        docData.setZhenDuanJL(zhenDuanJL);
    }

    private void setYongYao(Map<String, List<DaiMaDto>> shuYuMap,
                            JianKangTJVo jianKangTJ, JianKangTJ docData) {
        List<JkTiJianYYJLVo> jkTiJianYYJLList = jianKangTJ.getJkTiJianYYJLList();
        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_FYYCX);
        Map<String, String> yongYaoTJDMMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV06_00_102);

        // key: 名称
        Map<String, String> yongYaoTJMapBuValue = shuYuMap.getOrDefault(HealthDictionaryEnum.CV06_00_102.getCode(), Collections.emptyList())
                .stream().collect(Collectors.toMap(DaiMaDto::getValue, DaiMaDto::getCode));

        List<YongYao> yongYaoList = new ArrayList<>();
        for (JkTiJianYYJLVo e : jkTiJianYYJLList) {
            YongYao yongYao = new YongYao();

            YongYao.Item item = new YongYao.Item();

            item.setYaoPinFCX(new CodeObject(e.getFUYAOYCXDM(), daiMaDtoMap.get(e.getFUYAOYCXDM()).replace("规律服药", "规律")));

            // 获取单位
            String yyJL = strDefaultIfEmpty(e.getYONGLIANG(), "").replace(" ", "").replace("每次", "");
            // 单位
            String yyJLDW = strDefaultIfEmpty(getUnit(yyJL), "mg");
            // 数值
            String yyJlSZ = yyJL.replace(yyJLDW, "");

            try {
                item.setDanCiYYJL(new ValueUnit(new BigDecimal(yyJlSZ).setScale(2, RoundingMode.DOWN).toString(), yyJLDW));
            } catch (Exception ex) {
                item.setDanCiYYJL(new ValueUnit("0.00", "mg"));
            }
            item.setYaoPinMC(strDefaultIfEmpty(e.getYAOPINMC(), "无"));
            // TODO 为找到药品使用频率
            item.setYaoWuSYPL(new ValueUnit("0", "次/日"));

            // 直接搜索值域 - 根据名称匹配
            String yongYaoTJDM = strDefaultIfEmpty(yongYaoTJMapBuValue.get(e.getYONGFA()), "9");
            item.setYongYaoTJ(new CodeObject(yongYaoTJDM, yongYaoTJDMMap.get(yongYaoTJDM)));
            yongYao.getYongYaoItemList().add(item);
            yongYaoList.add(yongYao);
        }

        docData.setYongYao(yongYaoList);
    }

    /**
     * 获取单位
     *
     * @param value 10mg
     * @return mg
     */
    private static String getUnit(String value) {
        try {
            String reg = "([^0-9.]+?)";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(value.replace(" ", ""));
            boolean b = matcher.find();
            if (!b) {
                log.error("未获取到 {} 的单位", value);
                return value;
            }
            String group = matcher.group();
            return value.substring(value.indexOf(group));
        } catch (Exception ex) {
            return "mg";
        }
    }

    /**
     * 家庭关系映射
     */
    @Getter
    @AllArgsConstructor
    public enum JiaTingGXYS {
        BENREN("01", "01", "本人", "1", "男性"),
        PEIOU("10", "10", "配偶", "1", "男性"),

        FUQIN("905", "51", "父亲", "1", "男性"),
        MUQIN("906", "52", "母亲", "2", "女性"),
        // 兄弟姐妹
        XIONGDIJM("907", "80", "其他", "0", "未知的性别"),
        // 子女
        ZINV("908", "80", "其他", "0", "未知的性别"),
        OTHER("999", "80", "其他", "0", "未知的性别"),

        ;
        // 平台
        private final String pingTaiDM;
        // 国家
        private final String guoJiaDM;
        private final String guoJiaMC;
        private final String xingBieDM;
        private final String xingBie;


        public static JiaTingGXYS getByPingTaiDM(String pingTaiDM) {
            return Arrays.stream(values()).filter(e -> e.getPingTaiDM().equals(pingTaiDM)).findFirst().orElse(OTHER);
        }
    }

    private void setJiaZuS(Map<String, List<DaiMaDto>> shuYuMap,
                           JianKangTJVo jianKangTJ, JianKangTJ docData,
                           BingRenXXDto bingRenInfo) {
        List<JkGeRenJZSVo> list = jianKangTJ.getJkGeRenJZSList();
        JiaZuS jiaZuS = new JiaZuS();
        // 家族史没有数据的时候用本人
        if (CollectionUtils.isEmpty(list)) {
            JiaZuS.Item item = new JiaZuS.Item();
            item.setJiaTingGX(new CodeObject("01", "本人"));
            item.setXingBie(new CodeObject(bingRenInfo.getXingBieDM(), bingRenInfo.getXingBie()));
            item.getJiaZuJBList().add(new CodeObject("01", "无"));
        } else {
            Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV02_10_005);

            list.forEach(e -> {
                JiaTingGXYS guanXi = JiaTingGXYS.getByPingTaiDM(e.getYUBENRGX());
                JiaZuS.Item item = new JiaZuS.Item();
                item.setXingBie(new CodeObject(guanXi.getXingBieDM(), guanXi.getXingBie()));
                item.setJiaTingGX(new CodeObject(guanXi.getGuoJiaDM(), guanXi.getGuoJiaMC()));
                listDaiMa(e.getJIBINGDM()).forEach(e1 -> item.getJiaZuJBList().add(new CodeObject(e1, daiMaDtoMap.get(e1))));
                jiaZuS.getJiaZuSTMList().add(item);
            });
        }
        docData.setJiaZuS(jiaZuS);
    }

    private void setZhuYuanS(Map<String, List<DaiMaDto>> shuYuMap,
                             JianKangTJVo jianKangTJ, JianKangTJ docData) {
        List<JkZhuYuanSVo> jkZhuYuanSList = jianKangTJ.getJkZhuYuanSList();
        if (CollectionUtils.isEmpty(jkZhuYuanSList)) {
            log.warn("JkZhuYuanS is null");
            return;
        }

        List<ZhuYuanS> list = jkZhuYuanSList.stream().map(e -> {
            ZhuYuanS item = new ZhuYuanS();
            item.setRuYuanSJ(CDAUtil.toD8(e.getRUYUANRQ()));
            item.setChuYuanSJ(CDAUtil.toD8(e.getCHUYUANRQ()));
            item.setYiLiaoJGMC(e.getZHUYUANYLJGMC());

            item.setZhuYuanYYList(Collections.singletonList(e.getRUYUANYY()));
            // TODO 取现在的病案号?
            item.setCengZhuYBAHList(Collections.singletonList(e.getBINGANHAO()));
            return item;
        }).collect(Collectors.toList());

        docData.setZhuYuanSList(list);
    }

    private void setBingChuangS(Map<String, List<DaiMaDto>> shuYuMap,
                                JianKangTJVo jianKangTJ, JianKangTJ docData) {
        List<JkTiJianJTBCSVo> list = jianKangTJ.getJkTiJianJTBCSList();
        if (CollectionUtils.isEmpty(list)) {
            log.error("家庭病床史为空");
            return;
        }
        // 家庭病床
        List<BingChuangS> bingChuangSList = list.stream().map(e -> {
            BingChuangS item = new BingChuangS();
            item.setBingChuangJLYY(e.getJIANCHUANGYY());
            item.setCheChuangRQ(CDAUtil.toD8(e.getCHECHUANGRQ()));
            item.setJianChuangRQ(CDAUtil.toD8(e.getJIANCHUANGRQ()));
            item.setCengBingCBAH("-".equals(e.getBINGANHAO()) ? "0" : strDefaultIfEmpty(e.getBINGANHAO(), "0"));
            item.setYiLiaoJGMC(e.getYILIAOJGMC());
            return item;
        }).collect(Collectors.toList());
        docData.setBingChuangSList(bingChuangSList);
    }


    private void setYuFangJZ(Map<String, List<DaiMaDto>> shuYuMap,
                             JianKangTJVo jianKangTJ, JianKangTJ docData) {
        List<JkTiJianYFJZSVo> list = jianKangTJ.getJkTiJianYFJZSList();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("JkTiJianYFJZS is null");
            return;
        }
        List<YuFangJZ> resp = list.stream().map(e -> {
            // 预防接种
            YuFangJZ item = new YuFangJZ();
            item.setJieZhongRQ(strDefaultIfEmpty(CDAUtil.toD8(e.getJIEZHONGRQ()), CDAUtil.defaultD8()));
            item.setJieZhongJC(String.valueOf(e.getJIEZHONGJC() == null ? 0 : e.getJIEZHONGJC()));
            Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV08_50_001);
            String daiMa = strDefaultIfEmpty(e.getYUFANGJZYMDM(), "99");
            item.setYiMiaoBM(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

            item.setJieZhongBW("-");
            item.setYuFangJZYMMC(strDefaultIfEmpty(e.getYUFANGJZYMMC(), "-"));
            item.setYiMiaoPH(strDefaultIfEmpty(e.getYIMIAOPH(), "-"));
            item.setYiMiaoSCCJ(strDefaultIfEmpty(e.getYIMIAOSCCJ(), "-"));
            item.setJieZhongYS(strDefaultIfEmpty(e.getLURURXM(), "-"));
            item.setJieZhongJGMC(strDefaultIfEmpty(e.getYEWUJGMC(), "-"));
            return item;
        }).collect(Collectors.toList());
        docData.setYuFangJZList(resp);
    }


    @Getter
    @AllArgsConstructor
    public enum ZhiYeBLSEnum {
        FENGCHEN("1", "粉尘", "FENGCHEN", "FENGCHENFHCS"),
        FANGSHEWZ("5", "放射物质类", "FANGSHEWZ", "FANGSHEWZFHCS"),
        WULIYS("3", "物理有害因素", "WULIYS", "WULIYSFHCS"),
        HUAXUEWZ("2", "化学有害因素", "HUAXUEWZ", "HUAXUEWZFHCS"),
        DUWUZLQT("9", "其他", "DUWUZLQT", "DUWUZLQTFHCS"),

        ;
        private final String code;
        // 名称
        private final String mingCheng;
        private final String fieldName;
        // 防护措施字段名
        private final String fangHuCSZDM;
    }

    private void setZhiYeBLS(Map<String, List<DaiMaDto>> shuYuMap,
                             JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            log.warn("JkTiJianJBXX is null");
            return;
        }
        String zhiYeBLBZ = "2".equals(jkTiJianJBXX.getZHIYEBLBZ()) ? TRUE : FALSE;
        String congShiYWHYSGZ = strDefaultIfEmpty(jkTiJianJBXX.getCONGSHIYWHYSGZ(), "无");
        String congShiYWHYSCYSJ = strDefaultIfEmpty(jkTiJianJBXX.getCONGSHIYWHYSCYSJ(), "0");

        JSONObject obj = (JSONObject) JSON.toJSON(jkTiJianJBXX);
        List<ZhiYeBLS> list = Arrays.stream(ZhiYeBLSEnum.values())
                .filter(e -> StringUtils.isNotEmpty(obj.getString(e.getFieldName()))).map(e -> {
                    ZhiYeBLS item = new ZhiYeBLS();
                    item.setZhiYeBLBZ(zhiYeBLBZ);
                    item.setCongShiYWHYSGZ(congShiYWHYSGZ);
                    item.setCongShiYWHYSCYSJ(congShiYWHYSCYSJ);

                    item.setZhiYeBLWXYSZLDM(new CodeObject(e.getCode(), e.getMingCheng()));
                    item.setZhiYeBLWXYSMC(e.getMingCheng());
                    item.setZhiYeFHCSBZ("2".equals(obj.getString(e.getFangHuCSZDM())) ? TRUE : FALSE);
                    return item;
                }).collect(Collectors.toList());
        docData.getZhiYeBLSList().addAll(list);
    }

    private void setShengHuoFS(Map<String, List<DaiMaDto>> shuYuMap,
                               JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jbxx = jianKangTJ.getJkTiJianJBXX();
        if (jbxx == null) {
            log.warn("JkTiJianJBXX is null");
            return;
        }

        Map<String, String> daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV03_00_111);
        ShengHuoFS shengHuoFS = new ShengHuoFS();
        shengHuoFS.setDuanLianPL(new CodeObject(jbxx.getDUANLIANPL(), daiMaDtoMap.get(jbxx.getDUANLIANPL())));
        shengHuoFS.setMeiCiDLSJ(new ValueUnit(strDefaultIfEmpty(jbxx.getMEICIDLSJ(), null), "minutes"));
        shengHuoFS.setJianChiDLSJ(new ValueUnit(strDefaultIfEmpty(jbxx.getJIANCHIDLSJ(), null), "year"));
        shengHuoFS.setDuanLianFS("-".equals(jbxx.getDUANLIANFS()) ? "无锻炼方式" : strDefaultIfEmpty(jbxx.getDUANLIANFS(), "无锻炼方式"));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV03_00_107);
        jbxx.setYINSHIXG(jbxx.getYINSHIXG().split("\\|")[0]);
        shengHuoFS.setYinShiXG(new CodeObject(jbxx.getYINSHIXG(), daiMaDtoMap.get(jbxx.getYINSHIXG()) == null ? " " : daiMaDtoMap.get(jbxx.getYINSHIXG()).replace(" ", "")));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_XYZKDM);
        shengHuoFS.setXiYanZK(new CodeObject(jbxx.getXIYANQK(), daiMaDtoMap.get(jbxx.getXIYANQK())));
        shengHuoFS.setRiXiYL(new ValueUnit(strDefaultIfEmpty(jbxx.getRIXIYL(), null), "支"));
        shengHuoFS.setKaiShiXYNL(new ValueUnit(strDefaultIfEmpty(jbxx.getKAISHIXYNL(), null), "岁"));
        shengHuoFS.setJieYanNL(new ValueUnit(strDefaultIfEmpty(jbxx.getJIEYANNL(), null), "岁"));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV03_00_104);
        shengHuoFS.setYinJiuPL(new CodeObject(strDefaultIfEmpty(jbxx.getYINJIUPL(), null), daiMaDtoMap.get(jbxx.getYINJIUPL())));
        shengHuoFS.setRiYinJL(new ValueUnit(strDefaultIfEmpty(Converter.toInt(jbxx.getRIYINJL()), null), "两"));
        shengHuoFS.setJieJiuBZ("2".equals(jbxx.getJIEJIUBZ()) ? TRUE : FALSE);
        shengHuoFS.setJieJiuNL(new ValueUnit(strDefaultIfEmpty(jbxx.getJIEJIUNL(), null), "岁"));
        shengHuoFS.setKaiShiYJNL(new ValueUnit(strDefaultIfEmpty(jbxx.getKAISHIYJNL(), null), "岁"));
        shengHuoFS.setZuiJiuBZ("1".equals(String.valueOf(jbxx.getZUIJIUBZ())) ? TRUE : FALSE);

        if (!StringUtil.isNullOrWhiteSpace(jbxx.getYINJIUZL())) {
            daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV03_00_105);
            String daiMa = jbxx.getYINJIUZL().split("\\|")[0];
            shengHuoFS.setYinJiuZL(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
        }
        docData.setShengHuoFS(shengHuoFS);
    }

    private String strDefaultIfEmpty(Object obj, String defaultStr) {
        if (obj instanceof String) {
            if ("-".equals(obj)) {
                return defaultStr;
            }
            return StringUtils.defaultIfEmpty(String.valueOf(obj), defaultStr);
        } else {
            return Objects.isNull(obj) ? defaultStr : String.valueOf(obj);
        }
    }

    private <T> void processIfNotNull(T data, Consumer<T> consumer) {
        if (Objects.nonNull(data)) {
            consumer.accept(data);
        }
    }

    private void setJianKang(Map<String, List<DaiMaDto>> shuYuMap,
                             JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jbxx = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jbxx)) {
            log.warn("JkTiJianJBXX is null");
            return;
        }

        // 健康评估章节
        JianKangPG jianKangPG = new JianKangPG();
        String daiMa = "";
        Map<String, String> daiMaDtoMap;

        if (!"-".equals(jbxx.getLAONIANRJKZTZWPG())) {
            daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_01_013);
            jianKangPG.setJianKangZTPG(new CodeObject(jbxx.getLAONIANRJKZTZWPG(), daiMaDtoMap.get(jbxx.getLAONIANRJKZTZWPG())));
        }

        if (!"-".equals(jbxx.getLAONIANRSHZLNLZWPG())) {
            daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV04_01_014);
            jianKangPG.setShengHuoZLNLPG(new CodeObject(jbxx.getLAONIANRSHZLNLZWPG(), daiMaDtoMap.get(jbxx.getLAONIANRSHZLNLZWPG())));
        }

        if (!"-".equals(jbxx.getLAONIANRRZGN())) {
            daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_LNRRZGNCSJGDM);
            jianKangPG.setLaoNianRRZZTCSJG(new CodeObject(jbxx.getLAONIANRRZGN(), daiMaDtoMap.get(jbxx.getLAONIANRRZGN())));
            jianKangPG.setRenZhiNLPF(strDefaultIfEmpty(jbxx.getLAONIANRRZGNPF(), "0"));
        }

        if (!"-".equals(jbxx.getLAONIANRQGZT())) {
            daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_LNRQGZTCSJGDM);
            jianKangPG.setLaoNianRQGZTCSJG(new CodeObject(jbxx.getLAONIANRQGZT(), daiMaDtoMap.get(jbxx.getLAONIANRQGZT())));
            jianKangPG.setQingGanYYPF(strDefaultIfEmpty(jbxx.getLAONIANRYYPF(), "0"));
        }

        jianKangPG.setJianKangPJYCBZ("2".equals(String.valueOf(jbxx.getJIANKANGPJYCBZ())) ? TRUE : FALSE);
        jianKangPG.setJianKangPJYCXS(strDefaultIfEmpty(jbxx.getJIANKANGPJYCXS1(), "无"));
        docData.setJianKangPG(jianKangPG);

        // 健康指导章节
        JianKangZD jianKangZD = new JianKangZD();
        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CVX_JKZD);
        daiMa = listDaiMa(jbxx.getJIANKANGZDDM()).stream().findFirst().orElse("");
        jianKangZD.setJianKangZDDM(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));

        daiMaDtoMap = getDaiMaDtoMap(shuYuMap, HealthDictionaryEnum.CV06_00_218);
        daiMa = listDaiMa(jbxx.getTIJIANWXYSKZ()).stream().findFirst().orElse("9");
        jianKangZD.setTiJianWXYSKZ(new CodeObject(daiMa, daiMaDtoMap.get(daiMa)));
        docData.setJianKangZD(jianKangZD);
    }

    private Map<String, String> getDaiMaDtoMap(Map<String, List<DaiMaDto>> shuYuMap, HealthDictionaryEnum dictionaryEnum) {
        return shuYuMap.getOrDefault(dictionaryEnum.getCode(), Collections.emptyList())
                .stream().collect(Collectors.toMap(DaiMaDto::getCode, DaiMaDto::getValue));
    }


    /**
     * 症状代码映射枚举, 映射到国家代码
     * CVX_ZZDM -> ICD-10(默认值 Z00.001-健康查体)
     * <p>
     * 由于平台症状表中的代码和国家症状代码对应不上, 这里需要查询一下国家代码的所有数据, 然后根据平台代码值(名称)从国家代码值中匹配
     * <p>
     * 避免查询全部ICD-10太慢, 这里做映射, 降低查询的范围
     */
    @Getter
    @AllArgsConstructor
    public enum ZhengZhuangDMYSM {
        WUZHENGZ("无症状", "1", "Z00.001"),
        DUOYIN("多饮", "10", "R63.100x002"),
        DUONIAO("多尿", "11", "R35.x00"),
        TIZHONGXJ("体重下降", "12", "Z00.001"),
        FALI("乏力", "13", "R53.x00x002"),
        GUANJIEZT("关节肿痛", "14", "Z00.001"),
        SHILIMH("视力模糊", "15", "Z00.001"),
        SHOUJIAOMU("手脚麻木", "16", "Z00.001"),
        NIAOJI("尿急", "17", "R30.100x001"),
        NIAOTONG("尿痛", "18", "Z00.001"),
        BIANMI("便秘", "19", "K59.000"),
        TOUTONG("头痛", "2", "R51.x00"),
        FUXIE("腹泻", "20", "K52.916"),
        ERXINOT("恶心呕吐", "21", "Z00.001"),
        YANHUA("眼花", "22", "Z00.001"),
        ERMING("耳鸣", "23", "H93.100"),
        RUFANGZT("乳房胀痛", "24", "Z00.001"),
        TOUYUN("头晕", "3", "R42.x00x004"),
        XINJI("心悸", "4", "R00.200"),
        XIONGMEN("胸闷", "5", "R09.800x096"),
        XIONGTONG("胸痛", "6", "R07.400"),
        MANXINKS("慢性咳嗽", "7", "Z00.001"),
        KETAN("咳痰", "8", "Z00.001"),
        HUXIKUNN("呼吸困难", "9", "R06.000"),
        QITA("其他", "99", "Z00.001"),
        ;
        // 名称
        private final String mingCheng;
        private final String pingTaiDM;
        private final String guoJiaDM;

        public static ZhengZhuangDMYSM getByPingTaiDM(String pingTaiDM) {
            return Arrays.stream(values()).filter(e -> e.getPingTaiDM().equals(pingTaiDM)).findFirst().orElse(QITA);
        }

        public static Set<String> listGuoJiaDM() {
            return Arrays.stream(values()).map(ZhengZhuangDMYSM::getGuoJiaDM).collect(Collectors.toSet());
        }
    }

    private Map<String, List<DaiMaDto>> shuYuZhiYuQuery(BingRenXXDto bingRenXXDto, JianKangTJVo jianKangTJ) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();

        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        addValueItem(bingRenXXDto.getXingBieDM(), HealthDictionaryEnum.GB_T_2261_1, zhiJiBSFList);

        //症状
        if (!StringUtils.isEmpty(jkTiJianJBXX.getZHENZHUANG())) {
            Set<String> data = Arrays.stream(jkTiJianJBXX.getZHENZHUANG().split("\\|")).collect(Collectors.toSet());
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_ZZDM.getCode(), data);
        }
        // 老年人健康状态自我评估
        addValueItem(jkTiJianJBXX.getLAONIANRJKZTZWPG(), HealthDictionaryEnum.CV04_01_013, zhiJiBSFList);
        // 老年人生活自理能力自我评估
        addValueItem(jkTiJianJBXX.getLAONIANRSHZLNLZWPG(), HealthDictionaryEnum.CV04_01_014, zhiJiBSFList);
        // 锻炼频率
        addValueItem(jkTiJianJBXX.getDUANLIANPL(), HealthDictionaryEnum.CV03_00_111, zhiJiBSFList);
        // 饮食习惯
        addValueItem(jkTiJianJBXX.getDUANLIANPL(), HealthDictionaryEnum.CV03_00_107, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getYINSHIXG(), HealthDictionaryEnum.CV03_00_107, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getYINJIUPL(), HealthDictionaryEnum.CV03_00_104, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getYINJIUZL(), HealthDictionaryEnum.CV03_00_105, zhiJiBSFList);
        addValueItem(strDefaultIfEmpty(jkTiJianJBXX.getKOUCHUN(), "9"), HealthDictionaryEnum.CV04_10_007, zhiJiBSFList);
        addValueItem(strDefaultIfEmpty(jkTiJianJBXX.getCHILIE(), "1"), HealthDictionaryEnum.CV04_10_010, zhiJiBSFList);
        addValueItem(strDefaultIfEmpty(jkTiJianJBXX.getYANBU(), "1"), HealthDictionaryEnum.CVX_YB, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getTINGLI(), HealthDictionaryEnum.CVX_TINGLI, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getPIFU(), HealthDictionaryEnum.CV04_10_004, zhiJiBSFList);
        addValueItem(strDefaultIfEmpty(jkTiJianJBXX.getYUNDONGGN(), "1"), HealthDictionaryEnum.CVX_YDGN, zhiJiBSFList);
        addValueItem(strDefaultIfEmpty(jkTiJianJBXX.getXINGZHANGXLV(), "1"), HealthDictionaryEnum.CVX_XINLVQDM, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getXIYANQK(), HealthDictionaryEnum.CVX_XYZKDM, zhiJiBSFList);
        // 默认
        addValueItem("01", HealthDictionaryEnum.CV04_10_004, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getGONGMO(), HealthDictionaryEnum.CV04_10_006, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getLINGBAJ(), HealthDictionaryEnum.CV04_10_011, zhiJiBSFList); // 含有其他
        addValueItem(jkTiJianJBXX.getFEIBULY(), HealthDictionaryEnum.CVX_LYDM, zhiJiBSFList);
        // 默认
        addValueItem("01", HealthDictionaryEnum.CVX_LYDM, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getXIAZHISZ(), HealthDictionaryEnum.CV04_10_014, zhiJiBSFList);
        addValueItem("1", HealthDictionaryEnum.CV04_10_014, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getZUBEIDMBD(), HealthDictionaryEnum.CV04_10_015, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getGANGMENZZ(), HealthDictionaryEnum.CV04_10_013, zhiJiBSFList);
        // 默认值
        addValueItem("1", HealthDictionaryEnum.CV04_10_013, zhiJiBSFList);

        addValueItem(jkTiJianJBXX.getRUXIAN(), HealthDictionaryEnum.CV04_10_012, zhiJiBSFList);
        addValueItem("1", HealthDictionaryEnum.CV04_10_012, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getJIANKANGZDDM(), HealthDictionaryEnum.CVX_JKZD, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getTIJIANWXYSKZ(), HealthDictionaryEnum.CV06_00_218, zhiJiBSFList);
        addValueItem("9", HealthDictionaryEnum.CV06_00_218, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getLAONIANRRZGN(), HealthDictionaryEnum.CVX_LNRRZGNCSJGDM, zhiJiBSFList);
        addValueItem(jkTiJianJBXX.getLAONIANRQGZT(), HealthDictionaryEnum.CVX_LNRQGZTCSJGDM, zhiJiBSFList);

        // 辅助检测
        Optional.ofNullable(jianKangTJ.getJkTiJianFZJCList()).orElse(Collections.emptyList()).forEach(e -> {
            addValueItem(e.getNIAODANBBDXJCJGDM(), HealthDictionaryEnum.CV04_50_015, zhiJiBSFList);
            addValueItem(e.getNIAOTANGDXJCJGDM(), HealthDictionaryEnum.CV04_50_015, zhiJiBSFList);
            addValueItem(e.getNIAOTONGTDXJCJGDM(), HealthDictionaryEnum.CV04_50_015, zhiJiBSFList);
            addValueItem(e.getNIAOQIANXDXJCJGDM(), HealthDictionaryEnum.CV04_50_015, zhiJiBSFList);
        });

        // 体检现存主要健康问题 JK_TIJIANXCZYJKWT
        Optional.ofNullable(jianKangTJ.getJkTiJianXCZYJKWTList()).orElse(Collections.emptyList()).forEach(e -> {
            addValueItem(e.getNAOXUEGJB(), HealthDictionaryEnum.CVX_NXGJB, zhiJiBSFList);
            addValueItem(e.getSHENZANGJB(), HealthDictionaryEnum.CVX_GZJB, zhiJiBSFList);
            addValueItem(e.getXINZANGJB(), HealthDictionaryEnum.CVX_XZJB, zhiJiBSFList);
            addValueItem(e.getXUEGUANJB(), HealthDictionaryEnum.CVX_XGJB, zhiJiBSFList);
        });

        // 体检用药记录 JK_TIJIANYYJL
        Optional.ofNullable(jianKangTJ.getJkTiJianYYJLList()).orElse(Collections.emptyList()).forEach(e -> {
            addValueItem(e.getFUYAOYCXDM(), HealthDictionaryEnum.CVX_FYYCX, zhiJiBSFList);
        });

        // 老年人中医药健康管理
        for (JkLaoNianRZYJKGLVo item : jianKangTJ.getJkLaoNianRZYJKGLList()) {
            addValueItem(item.getZHUYAOTZ(), HealthDictionaryEnum.CV05_01_005, zhiJiBSFList);
            addValueItem(item.getQINGXIANGTZ(), HealthDictionaryEnum.CVX_ZYTZFLPD, zhiJiBSFList);
        }

        for (JkGeRenJZSVo item : jianKangTJ.getJkGeRenJZSList()) {
            addValueItem(item.getJIBINGDM(), HealthDictionaryEnum.CV02_10_005, zhiJiBSFList);
        }

        for (JkTiJianYFJZSVo item : jianKangTJ.getJkTiJianYFJZSList()) {
            addValueItem(String.valueOf(item.getYUFANGJZYMDM()), HealthDictionaryEnum.CV08_50_001, zhiJiBSFList);
            addValueItem("99", HealthDictionaryEnum.CV08_50_001, zhiJiBSFList);
        }

        ZhengZhuangDMYSM.listGuoJiaDM().forEach(e -> addValueItem(e, HealthDictionaryEnum.ICD_10, zhiJiBSFList));
        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));

        bingRenXXDto.setXingBie(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_2261_1.getCode(), Converter.toString(bingRenXXDto.getXingBieDM()), valueByCode));

        // 用药途径代码表
        Map<String, List<DaiMaDto>> daiMaByZJBSFList = shuYuService.getDaiMaByZJBSFList(Collections.singleton(HealthDictionaryEnum.CV06_00_102.getCode()));
        valueByCode.putAll(daiMaByZJBSFList);
        return valueByCode;
    }

    private List<String> listDaiMa(String daiMa) {
        if (!StringUtils.isEmpty(daiMa)) {
            return Arrays.stream(daiMa.split("\\|")).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    private void addValueItem(String daiMa, HealthDictionaryEnum dictionaryEnum, List<ZhiYuMXCXDto> zhiJiBSFList) {
        if (!StringUtils.isEmpty(daiMa)) {
            Set<String> codeSet = Arrays.stream(daiMa.split("\\|")).collect(Collectors.toSet());
            ZhiJiUtils.addValueItem(zhiJiBSFList, dictionaryEnum.getCode(), codeSet);
        }
    }

    private void setLegalAuthenticator(JianKangTJVo jianKangTJ, JianKangTJ docData) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();
        if (Objects.isNull(jkTiJianJBXX)) {
            return;
        }
        LegalAuthenticator legalAuth = new LegalAuthenticator();
        legalAuth.setTime(CDAUtil.toD8(jkTiJianJBXX.getTIJIANRQ()));
        legalAuth.setName(jkTiJianJBXX.getZERENYSXM());
        docData.setLegalAuthenticator(legalAuth);
    }

    void setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData, JianKangTJVo jianKangTJ,
                                 BingRenXXDto bingRenXXDto,
                                 HospitalDTO hospital) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJ.getJkTiJianJBXX();

        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(jkTiJianJBXX.getTIJIANRQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(bingRenXXDto));
        clinicalDocumentData.setAuthor(createAuthor(jianKangTJ, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(jkTiJianJBXX.getTIJIANBH());
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(JianKangTJVo jianKangTJVo, HospitalDTO hospital) {
        JkTiJianJBXXVo jkTiJianJBXX = jianKangTJVo.getJkTiJianJBXX();

        Author author = new Author();
        author.setTime(CDAUtil.toD15(jkTiJianJBXX.getTIJIANRQ()));
        author.setId(jkTiJianJBXX.getZERENYSDM());
        author.setName(jkTiJianJBXX.getZERENYSXM());
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(BingRenXXDto bingRenXXDto) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(bingRenXXDto.getEid()));
        patientRole.setTelecom(bingRenXXDto.getShouJiHao());
        patientRole.setAddr(createAddr(bingRenXXDto));
        patientRole.setPatient(createPatient(bingRenXXDto));
        return patientRole;
    }

    Addr createAddr(BingRenXXDto bingRenXXDto) {
        Addr addr = new Addr();
        addr.setState(bingRenXXDto.getJuZhuDSFMC());
        addr.setCity(bingRenXXDto.getJuZhuDSMC());
        addr.setCounty(bingRenXXDto.getJuZhuDQMC());
        addr.setTownship(bingRenXXDto.getJuZhuDJDMC());
        addr.setStreetName(bingRenXXDto.getJuZhuDSQMC());
        addr.setHouseNumber(bingRenXXDto.getJuZhuDMPH());
        addr.setPostalCode(bingRenXXDto.getLianXiRYB());
        return addr;
    }

    Patient createPatient(BingRenXXDto bingRenXXDto) {
        Patient patient = new Patient();
        patient.setId(bingRenXXDto.getZhengJianHM());
        patient.setName(bingRenXXDto.getBingRenXM());
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getXingBieDM()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getXingBie())) {
            patient.setAdministrativeGender(new CodeObject(bingRenXXDto.getXingBieDM(), bingRenXXDto.getXingBie()));
        }
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getChuShengRQ())) {
            patient.setBirthTime(CDAUtil.toD8(Converter.toDate(bingRenXXDto.getChuShengRQ())));
        }
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getHunYinZT()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getHunYinZTMC())) {
            patient.setMaritalStatus(new CodeObject(bingRenXXDto.getHunYinZT(), bingRenXXDto.getHunYinZTMC()));
        }
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuDM()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuMC())) {
            patient.setEthnicGroup(new CodeObject(bingRenXXDto.getMinZuDM(), bingRenXXDto.getMinZuMC()));
        }
        return patient;
    }

    private void setDefaultJianKangTJ(JianKangTJ jianKangTJ) {
        if (CollectionUtils.isEmpty(jianKangTJ.getZhengZhuangList())) {
            List<ZhengZhuang> zhengZhuangList = new ArrayList<>();
            ZhengZhuang zz = new ZhengZhuang();
            zz.setKaiShiRQ(CDAUtil.defaultD15());
            zz.setZhengZhuangDM("Z00.001");
            zz.setZhengZhuangMC("健康查体");
            zz.setZhengZhuangCXSJ("10");
            zhengZhuangList.add(zz);
            jianKangTJ.setZhengZhuangList(zhengZhuangList);
        }
        if (jianKangTJ.getShengMingTZ() == null) {
            ShengMingTZ shengMingTZ = new ShengMingTZ();
            shengMingTZ.setMaiLV("70");
            shengMingTZ.setShenGao("160.0");
            shengMingTZ.setTiWen("36.7");
            shengMingTZ.setTiZhong("58.0");
            shengMingTZ.setHuXiPL("20");
            shengMingTZ.setTiZhiZS("22.10");
            shengMingTZ.setXinLv("70");
            shengMingTZ.setYaoWei("79.0");
            shengMingTZ.setYouCeShuZhangY("88");
            shengMingTZ.setZuoCeShuZhangY("90");
            shengMingTZ.setYouCeShouSuoY("152");
            shengMingTZ.setZuoCeShouSuoY("159");
            jianKangTJ.setShengMingTZ(shengMingTZ);
        } else {
            ShengMingTZ shengMingTZ = jianKangTJ.getShengMingTZ();
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getMaiLV())) {
                shengMingTZ.setMaiLV("70");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getShenGao())) {
                shengMingTZ.setShenGao("160.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getTiWen())) {
                shengMingTZ.setTiWen("36.7");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getTiZhong())) {
                shengMingTZ.setTiZhong("58.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getHuXiPL())) {
                shengMingTZ.setHuXiPL("20");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getTiZhiZS())) {
                shengMingTZ.setTiZhiZS("22.10");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getXinLv())) {
                shengMingTZ.setXinLv("70");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getYaoWei())) {
                shengMingTZ.setYaoWei("79.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getYouCeShuZhangY())) {
                shengMingTZ.setYouCeShuZhangY("88");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getZuoCeShuZhangY())) {
                shengMingTZ.setZuoCeShuZhangY("90");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getYouCeShouSuoY())) {
                shengMingTZ.setYouCeShouSuoY("152");
            }
            if (StringUtil.isNullOrWhiteSpace(shengMingTZ.getZuoCeShouSuoY())) {
                shengMingTZ.setZuoCeShouSuoY("159");
            }
        }
        if (jianKangTJ.getKouQiang() == null) {
            KouQiang kouQiang = new KouQiang();
            kouQiang.setKouChun(new CodeObject("1", "红润"));
            kouQiang.setChiLie(new CodeObject("2", "缺齿"));
            kouQiang.setChiLieMS("缺齿");
            kouQiang.setYanBu(new CodeObject("1", "无充血"));
        }
        if (jianKangTJ.getYanBu() == null) {
            YanBu yanBu = new YanBu();
            yanBu.setYanDi("false");
            yanBu.setYanDiYCXS("-");
            yanBu.setGongMo(new CodeObject("1", "正常"));
            yanBu.setYouYanJZYSLZ("-9.0");
            yanBu.setYouYanLYYSLZ("4.1");
            yanBu.setZuoYanJZYSLZ("-9.0");
            yanBu.setZuoYanLYYSLZ("4.1");
            jianKangTJ.setYanBu(yanBu);
        }
        if (jianKangTJ.getTingLi() == null) {
            jianKangTJ.setTingLi(new CodeObject("1", "听见"));
        }
        if (jianKangTJ.getFuBu() == null) {
            FuBu fuBu = new FuBu();
            fuBu.setFuBuBKXS("无");
            fuBu.setFuBuBK(FALSE);
            fuBu.setFuBuYT(FALSE);
            fuBu.setFuBuYTXS("无");
            fuBu.setFuBuGD(FALSE);
            fuBu.setFuBuGDXS("无");
            fuBu.setFuBuPD(FALSE);
            fuBu.setFuBuPDXS(FALSE);
            fuBu.setFuBuYDXZY(FALSE);
            fuBu.setFuBuYDXZYXS("无");
        }
        if (jianKangTJ.getGongNengJC() == null) {
            GongNengJC gongNengJC = new GongNengJC();
            gongNengJC.setYunDongQK(new CodeObject("1", "可顺利完成"));
        }
        if (jianKangTJ.getXinZhang() == null) {
            XinZhang xinZhang = new XinZhang();
            xinZhang.setXinZhangXL(new CodeObject("2", "心律不齐"));
            xinZhang.setXinZhangZY(FALSE);
            xinZhang.setXinZhangZYXS("无");
            jianKangTJ.setXinZhang(xinZhang);
        }
        if (jianKangTJ.getXueGuan() == null) {
            XueGuan xueGuan = new XueGuan();
            xueGuan.setZuBeiDMBD(new CodeObject("2", "触及双侧对称"));
            jianKangTJ.setXueGuan(xueGuan);
        }
        if (jianKangTJ.getHuXiXT() == null) {
            HuXiXT huXiXT = new HuXiXT();
            huXiXT.setFeiBuLYXS("无");
            huXiXT.setFeiBuLY(FALSE);
            huXiXT.setTongZhuangGXBZ(FALSE);
            huXiXT.setFeiBuHXY(FALSE);
            huXiXT.setFeiBuHXYYCXS("无");
            jianKangTJ.setHuXiXT(huXiXT);
        }
        if (jianKangTJ.getPiFu() == null) {
            PiFu piFu = new PiFu();
            piFu.setPiFuJC(new CodeObject("01", "未见异常"));
            jianKangTJ.setPiFu(piFu);
        }
        if (jianKangTJ.getLinBaJC() == null) {
            jianKangTJ.setLinBaJC(new CodeObject("1", "未触及"));
        }
        if (jianKangTJ.getXiaZhiSZ() == null) {
            jianKangTJ.setXiaZhiSZ(new CodeObject("1", "无下肢水肿"));
        }
        if (jianKangTJ.getGangMenZZ() == null) {
            jianKangTJ.setGangMenZZ(new CodeObject("1", "未及异常"));
        }
        if (jianKangTJ.getRuXian() == null) {
            jianKangTJ.setRuXian(new CodeObject("1", "未见异常"));
        }
        if (jianKangTJ.getShengZhiQ() == null) {
            ShengZhiQ shengZhiQ = new ShengZhiQ();
            shengZhiQ.setFuKeFJYCXS("无");
            shengZhiQ.setFuKeFJ(FALSE);
            shengZhiQ.setFuKeGTYCXS("无");
            shengZhiQ.setFuKeGT(FALSE);
            shengZhiQ.setFuKeGJ(FALSE);
            shengZhiQ.setFuKeGJYCXS("无");
            shengZhiQ.setFuKeYD(FALSE);
            shengZhiQ.setFuKeYDYCXS("无");
            shengZhiQ.setFuKeWY(FALSE);
            shengZhiQ.setFuKeWYYCXS("无");
            jianKangTJ.setShengZhiQ(shengZhiQ);
        }
        if (jianKangTJ.getShiYanSJC() == null) {
            ShiYanSJC shiYanSJC = new ShiYanSJC();
            shiYanSJC.setXueHongDB("99");
            shiYanSJC.setBaiXiB("8.0");
            shiYanSJC.setXueXiaoB("101");
            shiYanSJC.setNiaoDanBBDXJCJG(new CodeObject("01", "-"));
            shiYanSJC.setNiaoTongTDXJCJG(new CodeObject("01", "-"));
            shiYanSJC.setNiaoQianXDXJCJG(new CodeObject("01", "-"));
            shiYanSJC.setKongFuXT("5.1");
            shiYanSJC.setNiaoWeiLBDB("100.1");
            shiYanSJC.setDaBianQX("ture");
            shiYanSJC.setTangHuaXHDB("99.0");
            shiYanSJC.setXueQingGBZAM("-99");
            shiYanSJC.setXueQingGCZAM("-99");
            shiYanSJC.setBaiDanB("-99");
            shiYanSJC.setZongDanHS("-99");
            shiYanSJC.setJieHeDHS("-99");
            shiYanSJC.setXueQingJG("9.0");
            shiYanSJC.setXueNiaoSU("-99.0");
            shiYanSJC.setXueJiaND("89.0");
            shiYanSJC.setXueNaND("79.00");
            shiYanSJC.setZongDanGC("-99.00");
            shiYanSJC.setGanYouSZ("-99.00");
            shiYanSJC.setXueQingDMDZDBDGC("9.0");
            shiYanSJC.setXueQingGMDZDBDGC("-99.00");

            shiYanSJC.setYiGanGYBMKY(new CodeObject("1", "阴性"));
            shiYanSJC.setYiXingGDBMKT(new CodeObject("1", "阴性"));
            shiYanSJC.setYiXingGDEKY(new CodeObject("1", "阴性"));
            shiYanSJC.setYiXingGYEKT(new CodeObject("1", "阴性"));
            shiYanSJC.setYiXingGDHXKT(new CodeObject("1", "阴性"));
            shiYanSJC.setNiaoTangDXJCJG(new CodeObject("01", "-"));
            jianKangTJ.setShiYanSJC(shiYanSJC);
        } else {
            ShiYanSJC shiYanSJC = jianKangTJ.getShiYanSJC();
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueHongDB())) {
                shiYanSJC.setXueHongDB("99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getBaiXiB())) {
                shiYanSJC.setBaiXiB("8.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueXiaoB())) {
                shiYanSJC.setXueXiaoB("101");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getNiaoDanBBDXJCJG().getCode())) {
                shiYanSJC.setNiaoDanBBDXJCJG(new CodeObject("01", "-"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getNiaoTongTDXJCJG().getCode())) {
                shiYanSJC.setNiaoTongTDXJCJG(new CodeObject("01", "-"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getNiaoQianXDXJCJG().getCode())) {
                shiYanSJC.setNiaoQianXDXJCJG(new CodeObject("01", "-"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getKongFuXT())) {
                shiYanSJC.setKongFuXT("5.1");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getNiaoWeiLBDB())) {
                shiYanSJC.setNiaoWeiLBDB("100.1");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getDaBianQX())) {
                shiYanSJC.setDaBianQX("ture");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getTangHuaXHDB())) {
                shiYanSJC.setTangHuaXHDB("99.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueQingGBZAM())) {
                shiYanSJC.setXueQingGBZAM("-99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueQingGCZAM())) {
                shiYanSJC.setXueQingGCZAM("-99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getBaiDanB())) {
                shiYanSJC.setBaiDanB("-99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getZongDanHS())) {
                shiYanSJC.setZongDanHS("-99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getJieHeDHS())) {
                shiYanSJC.setJieHeDHS("-99");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueQingJG())) {
                shiYanSJC.setXueQingJG("9.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueNiaoSU())) {
                shiYanSJC.setXueNiaoSU("-99.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueJiaND())) {
                shiYanSJC.setXueJiaND("89.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueNaND())) {
                shiYanSJC.setXueNaND("79.00");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getZongDanGC())) {
                shiYanSJC.setZongDanGC("-99.00");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getGanYouSZ())) {
                shiYanSJC.setGanYouSZ("-99.00");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueQingDMDZDBDGC())) {
                shiYanSJC.setXueQingDMDZDBDGC("9.0");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getXueQingGMDZDBDGC())) {
                shiYanSJC.setXueQingGMDZDBDGC("-99.00");
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getYiGanGYBMKY().getCode())) {
                shiYanSJC.setYiGanGYBMKY(new CodeObject("1", "阴性"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getYiXingGDBMKT().getCode())) {
                shiYanSJC.setYiXingGDBMKT(new CodeObject("1", "阴性"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getYiXingGDEKY().getCode())) {
                shiYanSJC.setYiXingGDEKY(new CodeObject("1", "阴性"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getYiXingGYEKT().getCode())) {
                shiYanSJC.setYiXingGYEKT(new CodeObject("1", "阴性"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getYiXingGDHXKT().getCode())) {
                shiYanSJC.setYiXingGDHXKT(new CodeObject("1", "阴性"));
            }
            if (StringUtil.isNullOrWhiteSpace(shiYanSJC.getNiaoTangDXJCJG().getCode())) {
                shiYanSJC.setNiaoTangDXJCJG(new CodeObject("01", "-"));
            }
        }
        if (jianKangTJ.getFuZhuJC() == null) {
            FuZhuJC fuZhuJC = new FuZhuJC();
            fuZhuJC.setXiongBUXXP(FALSE);
            fuZhuJC.setXiongBuXXPYCXS("无");
            fuZhuJC.setXinDianT(FALSE);
            fuZhuJC.setXinDianTYCXS("窦性心律不齐");
            fuZhuJC.setFuBuBC(FALSE);
            fuZhuJC.setFuBuBCYCXS("无");
            fuZhuJC.setGongJingTP(FALSE);
            fuZhuJC.setGongJingTPYCXS("无");
            jianKangTJ.setFuZhuJC(fuZhuJC);
        }
        if (jianKangTJ.getZhenDuanJL() == null) {
            ZhenDuanJL zhenDuanJL = new ZhenDuanJL();
            zhenDuanJL.setXianCunZYJKWT(new CodeObject("J11.101", "流行性感冒"));
            jianKangTJ.setZhenDuanJL(zhenDuanJL);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getZhenDuanJL().getZhongYiTZList())) {
            ZhenDuanJL.ZhongYiTZ zhongYiT = new ZhenDuanJL.ZhongYiTZ();
            zhongYiT.setTiZhiFL(new CodeObject("02", "气虚质"));
            zhongYiT.setTiZhiFLPDJG(new CodeObject("3", "倾向是"));
            jianKangTJ.getZhenDuanJL().getZhongYiTZList().add(zhongYiT);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getYongYao())) {
            List<YongYao> yongYaoList = new ArrayList<>();
            YongYao yongYao = new YongYao();
            //没有记录 填默认值
            YongYao.Item item = new YongYao.Item();
            item.setYaoPinFCX(new CodeObject("1", "规律"));
            item.setDanCiYYJL(new ValueUnit("0.00", "mg"));
            item.setYaoPinMC("无");
            item.setYaoWuSYPL(new ValueUnit("0", "次/日"));
            item.setYongYaoTJ(new CodeObject("9", "其他用药途径"));
            yongYao.getYongYaoItemList().add(item);
            yongYaoList.add(yongYao);
            jianKangTJ.setYongYao(yongYaoList);
        }
        if (jianKangTJ.getJiaZuS() == null) {
            JiaZuS jiaZuS = new JiaZuS();
            // 家族史没有数据的时候用本人
            JiaZuS.Item item = new JiaZuS.Item();
            item.setJiaTingGX(new CodeObject("01", "本人"));
            item.setXingBie(new CodeObject("1", "男性"));
            item.getJiaZuJBList().add(new CodeObject("01", "无"));
            jianKangTJ.setJiaZuS(jiaZuS);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getZhuYuanSList())) {
            jianKangTJ.setZhuYuanSList(new ArrayList<>());
            ZhuYuanS item = new ZhuYuanS();
            item.setRuYuanSJ(CDAUtil.defaultD8());
            item.setChuYuanSJ(CDAUtil.defaultD8());
            item.setYiLiaoJGMC("-");
            item.setZhuYuanYYList(Collections.singletonList("前列腺癌手术"));
            item.setCengZhuYBAHList(Collections.singletonList("00000"));
            jianKangTJ.getZhuYuanSList().add(item);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getBingChuangSList())) {
            jianKangTJ.setBingChuangSList(new ArrayList<>());
            // 家庭病床
            BingChuangS item = new BingChuangS();
            item.setBingChuangJLYY("-");
            item.setCheChuangRQ(CDAUtil.defaultD8());
            item.setJianChuangRQ(CDAUtil.defaultD8());
            item.setCengBingCBAH("00000");
            item.setYiLiaoJGMC("-");
            jianKangTJ.getBingChuangSList().add(item);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getYuFangJZList())) {
            jianKangTJ.setYuFangJZList(new ArrayList<>());
            // 预防接种
            YuFangJZ item = new YuFangJZ();
            item.setJieZhongRQ(CDAUtil.defaultD8());
            item.setJieZhongJC("1");
            item.setYiMiaoBM(new CodeObject("12", "水痘疫苗"));
            item.setJieZhongBW("-");
            item.setYuFangJZYMMC("水痘疫苗");
            item.setYiMiaoPH("-");
            item.setYiMiaoSCCJ("-");
            item.setJieZhongYS("-");
            item.setJieZhongJGMC("-");
            jianKangTJ.getYuFangJZList().add(item);
        }
        if (CollectionUtils.isEmpty(jianKangTJ.getZhiYeBLSList())) {
            jianKangTJ.setZhiYeBLSList(new ArrayList<>());
            ZhiYeBLS item = new ZhiYeBLS();
            item.setZhiYeBLBZ("true");
            item.setCongShiYWHYSGZ("从事有危害因素工种");
            item.setCongShiYWHYSCYSJ("1");
            item.setZhiYeBLWXYSZLDM(new CodeObject("1", "粉尘"));
            item.setZhiYeBLWXYSMC("粉尘");
            item.setZhiYeFHCSBZ(FALSE);
            jianKangTJ.getZhiYeBLSList().add(item);
        }
        if (jianKangTJ.getShengHuoFS() == null) {
            ShengHuoFS shengHuoFS = new ShengHuoFS();
            shengHuoFS.setDuanLianPL(new CodeObject("1", "每天"));
            shengHuoFS.setMeiCiDLSJ(new ValueUnit("20", "minutes"));
            shengHuoFS.setJianChiDLSJ(new ValueUnit("10", "year"));
            shengHuoFS.setDuanLianFS("无锻炼方式");
            shengHuoFS.setYinShiXG(new CodeObject("1", "荤素均衡"));
            shengHuoFS.setXiYanZK(new CodeObject("3", "吸烟"));
            shengHuoFS.setRiXiYL(new ValueUnit("10", "支"));
            shengHuoFS.setKaiShiXYNL(new ValueUnit("20", "岁"));
            shengHuoFS.setJieYanNL(new ValueUnit("40", "岁"));
            shengHuoFS.setYinJiuPL(new CodeObject("4", "每天"));
            shengHuoFS.setRiYinJL(new ValueUnit("2", "两"));
            shengHuoFS.setJieJiuBZ(FALSE);
            shengHuoFS.setJieJiuNL(new ValueUnit("40", "岁"));
            shengHuoFS.setKaiShiYJNL(new ValueUnit("20", "岁"));
            shengHuoFS.setZuiJiuBZ(FALSE);
            shengHuoFS.setYinJiuZL(new CodeObject("1", "白酒"));
            jianKangTJ.setShengHuoFS(shengHuoFS);
        }
        if (jianKangTJ.getJianKangPG() == null) {
            JianKangPG jianKangPG = new JianKangPG();
            jianKangPG.setJianKangZTPG(new CodeObject("1", "满意"));
            jianKangPG.setShengHuoZLNLPG(new CodeObject("1", "可自理"));
            jianKangPG.setLaoNianRRZZTCSJG(new CodeObject("1", "粗筛阴性"));
            jianKangPG.setLaoNianRQGZTCSJG(new CodeObject("1", "粗筛阴性"));
            jianKangPG.setQingGanYYPF("0");
            jianKangPG.setRenZhiNLPF("0");
            jianKangPG.setJianKangPJYCBZ(FALSE);
            jianKangPG.setJianKangPJYCXS("窦性心律不齐");
            jianKangTJ.setJianKangPG(jianKangPG);
        }
        if (jianKangTJ.getJianKangZD() == null) {
            JianKangZD jianKangZD = new JianKangZD();
            jianKangZD.setJianKangZDDM(new CodeObject("2", "建议复查"));
            jianKangZD.setTiJianWXYSKZ(new CodeObject("1", "戒烟"));
            jianKangTJ.setJianKangZD(jianKangZD);
        }
    }
}
